package com.df.springmvc.workflow.api.controller;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.df.springmvc.common.bean.MsgBean;
import com.df.springmvc.common.util.WebUtil;
import com.df.springmvc.supper.SupperController;
import com.df.springmvc.workflow.api.services.APIServices;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

@Controller
@RequestMapping("/workflow/api/API")
/**
 * 本类仅做一些简单数据处理如:
 *格式验证
 *数据封装
 * @author Turner
 *
 */
public class APIController extends SupperController{
	String page="workflow/api/" + this.getClass().getSimpleName().replace("Controller","") + "_";
	@Autowired
	APIServices services;
	
	/**
	 * 发起流程
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/doNew")
	public void doNew(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			HashMap<String,Object> jsonMap=new Gson().fromJson(getParam(request,"json"),new HashMap<String,Object>().getClass());
			if(jsonMap == null){
				jsonMap=new HashMap<String,Object>(getParamMap(request));
			}
			HashMap<String,Object> paramMap=new HashMap<String,Object>();
			String proc_def_key="" + jsonMap.get("proc_def_key");
			String form_guid="" + jsonMap.get("act_form_guid");
			String title="" + jsonMap.get("title");
			if(jsonMap.get("map") != null){
				paramMap=new HashMap<String,Object>((LinkedTreeMap<String,Object>)jsonMap.get("map"));
			}
			Object rs=services.doNew(proc_def_key,form_guid,title,paramMap);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	/**
	 * 签收@param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/doClaim")
	public void doClaim(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			HashMap<String,Object> jsonMap=new Gson().fromJson(getParam(request,"json"),new HashMap<String,Object>().getClass());
			if(jsonMap == null){
				jsonMap=new HashMap<String,Object>(getParamMap(request));
			}
			String taskId="" + jsonMap.get("act_form_taskId");
			String userId="" + jsonMap.get("userId");
			Object rs=services.doClaim(taskId,userId,new HashMap<String,Object>());
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	/**
	 * 完成
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/doComplete")
	public void doComplete(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			HashMap<String,Object> jsonMap=new Gson().fromJson(getParam(request,"json"),new HashMap<String,Object>().getClass());
			HashMap<String,Object> paramMap=new HashMap<String,Object>();
			if(jsonMap == null){
				jsonMap=new HashMap<String,Object>(getParamMap(request));
			}
			if(jsonMap.get("map") != null){
				paramMap=new HashMap<String,Object>((LinkedTreeMap<String,Object>)jsonMap.get("map"));
			}
			String taskId="" + jsonMap.get("act_form_taskId");
			String userId="" + jsonMap.get("userId");
			Object rs=services.doComplete(taskId,userId,paramMap);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	/**
	 * 转呈
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/doReAssignee")
	public void doReAssignee(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			HashMap<String,Object> jsonMap=new Gson().fromJson(getParam(request,"json"),new HashMap<String,Object>().getClass());
			if(jsonMap == null){
				jsonMap=new HashMap<String,Object>(getParamMap(request));
			}
			HashMap<String,Object> paramMap=new HashMap<String,Object>();
			if(jsonMap.get("map") != null){
				paramMap=new HashMap<String,Object>((LinkedTreeMap<String,Object>)jsonMap.get("map"));
			}
			String taskId="" + jsonMap.get("act_form_taskId");
			String userId="" + jsonMap.get("userId");
			Object rs=services.doReAssignee(taskId,userId,paramMap);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	/**
	 * 加签
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/doChain")
	public void doChain(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			HashMap<String,Object> jsonMap=new Gson().fromJson(getParam(request,"json"),new HashMap<String,Object>().getClass());
			if(jsonMap == null){
				jsonMap=new HashMap<String,Object>(getParamMap(request));
			}
			HashMap<String,Object> paramMap=new HashMap<String,Object>();
			if(jsonMap.get("map") != null){
				paramMap=new HashMap<String,Object>((LinkedTreeMap<String,Object>)jsonMap.get("map"));
			}
			String taskId="" + jsonMap.get("act_form_taskId");
			String userId="" + jsonMap.get("userId");
			Object rs=services.doChain(taskId,userId,paramMap);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	/**
	 * 使用GUID查询任务信息
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/queryTaskIdByGuid")
	public void queryTaskIdByGuid(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			HashMap<String,Object> jsonMap=new Gson().fromJson(getParam(request,"json"),new HashMap<String,Object>().getClass());
			if(jsonMap == null){
				jsonMap=new HashMap<String,Object>(getParamMap(request));
			}
			String form_guid="" + jsonMap.get("act_form_guid");
			Object rs=services.queryTaskIdByGuid(form_guid);
			org.apache.log4j.Logger.getLogger(getClass()).debug("queryTaskIdByGuid:" + form_guid);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	@RequestMapping("/queryTaskByTaskId")
	public void queryTaskByTaskId(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			HashMap<String,Object> jsonMap=new Gson().fromJson(getParam(request,"json"),new HashMap<String,Object>().getClass());
			if(jsonMap == null){
				jsonMap=new HashMap<String,Object>(getParamMap(request));
			}
			String form_taskId="" + jsonMap.get("act_form_taskId");
			org.apache.log4j.Logger.getLogger(getClass()).info("queryTaskByTaskId:" + form_taskId);
			Object rs=services.queryTaskByTaskId(form_taskId);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
}
